package com.xy.p2p.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import com.xy.core.util.BeanUtil;
import com.xy.core.util.DateUtil;
import com.xy.common.page.Pagination;
import com.xy.common.util.ErFen;
import com.xy.core.service.impl.BaseServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xy.p2p.dao.TfinanceinfoDao;
import com.xy.p2p.dao.TmemberassetaccountDao;
import com.xy.p2p.dao.TmemberassetaccountdetailsDao;
import com.xy.p2p.dao.TtenderrecordDao;
import com.xy.p2p.dao.TyedtransferoutDao;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Tmemberassetaccount;
import com.xy.p2p.model.Tmemberassetaccountdetails;
import com.xy.p2p.model.Tmemberrates;
import com.xy.p2p.model.Ttenderrecord;
import com.xy.p2p.model.Tuser;
import com.xy.p2p.model.Tyedtransferout;
import com.xy.p2p.service.TmemberService;
import com.xy.p2p.service.TmemberratesService;
import com.xy.p2p.service.TyedtransferoutService;

@Service
@Transactional
public class TyedtransferoutServiceImpl extends BaseServiceImpl<Tyedtransferout> implements
		TyedtransferoutService {
//	1：成功，0：失败，2：我方处理中
	private final int TYEDTRANSFEROUT_AUDIT_SUCCESS=1;
	private final int TYEDTRANSFEROUT_AUDIT_FAIL=0;
	
	
	
	private TyedtransferoutDao tyedtransferoutDao;
	@Autowired
	private TtenderrecordDao ttenderrecordDao;
	@Autowired
	private TfinanceinfoDao tfinanceinfoDao;
	@Autowired
	private TmemberassetaccountDao tmemberassetaccountDao;
	@Autowired
	private TmemberassetaccountdetailsDao tmemberassetaccountdetailsDao;
	@Autowired
	private TmemberratesService tmemberratesService;
	
	public TyedtransferoutServiceImpl(TyedtransferoutDao paramAppRoleDao) {
		super(paramAppRoleDao);
		this.tyedtransferoutDao = paramAppRoleDao;
	}

	@Override
	public boolean checkExist(String paraName, String paraValue) {
		return tyedtransferoutDao.checkExist("Tyedtransferout", paraName, paraValue);
	}

	@Override
	public List<Tyedtransferout> getTyedtransferout(Map<String, Object> map) {
		return tyedtransferoutDao.getTyedtransferout(map);
	}
	@Override
	public Pagination getTyedtransferoutByPage(Map<String, Object> map, int pageNo,
			int pageSize) {
		// TODO Auto-generated method stub
		return tyedtransferoutDao.getTyedtransferoutByPage(map, pageNo, pageSize);
	}
	 
   @Override
	public Tyedtransferout save(Tyedtransferout tyedtransferout) {
		if(tyedtransferout.getToid()==null){
			//新增
			Tyedtransferout temp = super.save(tyedtransferout);
        	return temp;
		}else{
			//更新
			Tyedtransferout tyedtransferout1=tyedtransferoutDao.get(tyedtransferout.getToid());
			try {
				BeanUtil.copyNotNullProperties(tyedtransferout1, tyedtransferout);
			} catch (Exception e) {
				e.printStackTrace();
			}  
			return super.save(tyedtransferout1);
		}
	}

   //转出审核
	@Override
	public Tyedtransferout audit(Tuser tuser, String opr,
			Tyedtransferout tyedtransferout) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("Tmember", tyedtransferout.getTmember());
		Tmemberassetaccount tmemberassetaccount = tmemberassetaccountDao.getTmemberassetaccount(queryMap).get(0);
		if (opr.equals("y")) {
			tyedtransferout.setStatus(TYEDTRANSFEROUT_AUDIT_SUCCESS);
			double outMoney=tyedtransferout.getToam();
			//减去投标记录的计息金额
			Ttenderrecord ttenderrecord = tyedtransferout.getTtenderrecord();
			ttenderrecord.setMibmoney(ttenderrecord.getMibmoney()-outMoney); 
			int monthsub = DateUtil.getMonthSpace(new Date(), tyedtransferout.getTtenderrecord().getTddatetime());
			int limit = tyedtransferout.getTtenderrecord().getTfinanceinfo().getFinanceallotedtime();
			//判断是否提前转出
			if( monthsub*30<limit){
				Tmemberrates rate = tmemberratesService.getYED_early(limit+"");
				double r = 0;
				if(null==rate){
					r = 0;
				}else{
					r = rate.getRates();
				}
				Tmemberassetaccountdetails tmemberassetaccountdetails = new Tmemberassetaccountdetails(tyedtransferout.getTmember(),outMoney, Tmemberassetaccountdetails.TMEMBERASSETACCOUNTDETAIL_BORROWING_IN,
						Tmemberassetaccountdetails.YED_CAPITAL_IN, new Date(),tyedtransferout.getTtenderrecord().getTfinanceinfo().getFiid().toString(), null);
				Tmemberassetaccountdetails tmemberassetaccountdetails2 = new Tmemberassetaccountdetails(tyedtransferout.getTmember(), outMoney*r, Tmemberassetaccountdetails.TMEMBERASSETACCOUNTDETAIL_BORROWING_OUT,
						Tmemberassetaccountdetails.YED_EARLY_SERVER_FEE, new Date(), "平台", null);
				tmemberassetaccountdetailsDao.save(tmemberassetaccountdetails);
				tmemberassetaccountdetailsDao.save(tmemberassetaccountdetails2);
				outMoney = outMoney*(1-r);
			}else{
				Tmemberassetaccountdetails tmemberassetaccountdetails = new Tmemberassetaccountdetails(tyedtransferout.getTmember(),outMoney, Tmemberassetaccountdetails.TMEMBERASSETACCOUNTDETAIL_BORROWING_IN,
						Tmemberassetaccountdetails.YED_CAPITAL_IN, new Date(),tyedtransferout.getTtenderrecord().getTfinanceinfo().getFiid().toString(), null);
				tmemberassetaccountdetailsDao.save(tmemberassetaccountdetails);
			}
			tmemberassetaccount.setBalance(tmemberassetaccount.getBalance()+outMoney);
			tmemberassetaccountDao.save(tmemberassetaccount);
			//融资人余额减少
			Tmember t = ttenderrecord.getTfinanceinfo().getTmember();
			Map<String, Object> qm = new HashMap<String, Object>();
			qm.put("Tmember", t);
			Tmemberassetaccount tac = tmemberassetaccountDao.getTmemberassetaccount(qm).get(0);
			tac.setBalance(tac.getBalance()-tyedtransferout.getToam());
			tmemberassetaccountDao.save(tac);
			Tmemberassetaccountdetails tcd = new Tmemberassetaccountdetails(t, tyedtransferout.getToam(), Tmemberassetaccountdetails.TMEMBERASSETACCOUNTDETAIL_BORROWING_OUT, Tmemberassetaccountdetails.YED_CAPITAL_OUT, new Date(), ttenderrecord.getTfinanceinfo().getFiid().toString(), null);
			tmemberassetaccountdetailsDao.save(tcd);
		} else {
			tyedtransferout.setStatus(TYEDTRANSFEROUT_AUDIT_FAIL);
		}
		
		tyedtransferout.setTuser(tuser);
		tyedtransferout.setRevieweddate(new Date());
		return tyedtransferoutDao.save(tyedtransferout);
	}
}
